package com.bocaizp.operation.service.impl;

import com.bocaizp.common.context.BaseContext;
import com.bocaizp.common.utils.AliOssUtil;
import com.bocaizp.operation.mapper.LoginMapper;
import com.bocaizp.operation.mapper.UserInfoMapper;
import com.bocaizp.operation.service.UserInfoService;
import com.bocaizp.pojo.DTO.ModifiedInfoDTO;
import com.bocaizp.pojo.VO.EmployeeVO;
import com.bocaizp.pojo.entity.Employee;
import com.bocaizp.pojo.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Author : huang jie
 * Date : 2024/5/7 17:53
 **/
@Service
@Slf4j
public class UserInfoServiceImpl implements UserInfoService {

	@Autowired
	private UserInfoMapper userInfoMapper;

	@Autowired
	private LoginMapper loginMapper;

	@Autowired
	private AliOssUtil aliOssUtil;

	/**
	 * 修改个人信息
	 * @param modifiedInfoDTO
	 * @return
	 */
	public void modifiedInfo(ModifiedInfoDTO modifiedInfoDTO) {
		modifiedInfoDTO.setId(BaseContext.getCurrentId());
		User user = new User();
		BeanUtils.copyProperties(modifiedInfoDTO, user);
		loginMapper.update(user);
	}

	/**
	 * 删除旧的头像信息（oss）
	 */
	public void delete(int flag) {
		Long id = BaseContext.getCurrentId();
		String fileName;
		if(flag == 0){
			fileName = userInfoMapper.getAvatar(id);
		}else{
			fileName = userInfoMapper.getBio(id);
		}
		if(fileName == null){
			return;
		}

		String name = fileName.substring(fileName.lastIndexOf('/') + 1);
		if(aliOssUtil.deleteFile(name)){
			System.out.println("删除成功！");

		}else{
			System.out.println("删除失败");
		}
	}

	/**
	 * 员工信息
	 * @return
	 */
	public List<EmployeeVO> getEmployeeInfo() {
		List<EmployeeVO> employeeVOList = userInfoMapper.getEmployeeInfo();
		return employeeVOList;
	}

	/**
	 * 新增员工
	 * @param employee
	 * @return
	 */
	public int addEmloyee(Employee employee) {
		userInfoMapper.addEmployee(employee);
		int id = employee.getEmpId();
		System.out.println(id);
		return id;
	}
}
